package com.aps.orchestrator.scheduler;

import com.aps.algorithm.core.APSAlgorithm;
import com.aps.algorithm.core.AlgorithmParameters;
import com.aps.algorithm.heuristic.genetic.dto.ApsOperationDto;
import com.aps.algorithm.heuristic.genetic.model.Chromosome;
import com.aps.common.model.APSProductModel;
import com.aps.common.model.ApsParameterModel;
import com.aps.common.model.SchedulingResult;
import com.aps.orchestrator.scenario.ProductionScenario;
import com.aps.orchestrator.scenario.ScenarioAnalyzer;
import com.aps.orchestrator.selector.AlgorithmSelector;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 算法调度器
 * 负责协调算法选择、执行和结果处理
 */
@Slf4j
@Component
public class AlgorithmScheduler {
    
    @Autowired
    private ScenarioAnalyzer scenarioAnalyzer;
    
    @Autowired
    private AlgorithmSelector algorithmSelector;

//    @Resource
//    private APSAlgorithm algorithm;

    /**
     * 执行排程
     * @param model APS产品模型
     * @param parameters 算法参数
     * @return 排程结果
     */
    public SchedulingResult schedule(ApsParameterModel model, AlgorithmParameters parameters) {
        log.info("开始执行算法调度");
        
        try {
//            // 1. 分析生产场景
//            ProductionScenario scenario = scenarioAnalyzer.analyzeScenario(model);
//
//            // 2. 选择算法
//            APSAlgorithm algorithm = algorithmSelector.selectAlgorithm(scenario);
            // 3. 执行算法
//            SchedulingResult result = algorithm.execute(model, parameters);
            
            // 4. 记录调度信息
//            logSchedulingInfo(scenario, algorithm, result);
            
            log.info("算法调度执行完成");
//            return result;
            return null;

        } catch (Exception e) {
            log.error("算法调度执行失败", e);
            throw new RuntimeException("算法调度执行失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 使用指定算法执行排程
     * @param model APS产品模型
     * @param algorithmName 算法名称
     * @param parameters 算法参数
     * @return 排程结果
     */
    public List<SchedulingResult> scheduleWithAlgorithm(ApsParameterModel model,
                                                 String algorithmName, 
                                                 AlgorithmParameters parameters) {
        log.info("使用指定算法执行排程: {}", algorithmName);
        
        try {
            // 获取指定算法
            APSAlgorithm algorithm = algorithmSelector.getAlgorithmByName(algorithmName);
            if (algorithm == null) {
                throw new IllegalArgumentException("未找到算法: " + algorithmName);
            }
            
            // 执行算法
            Chromosome<ApsOperationDto> execute = algorithm.execute(model, parameters);
            
            log.info("指定算法排程执行完成: {}", algorithmName);
            return null;
            
        } catch (Exception e) {
            log.error("指定算法排程执行失败: {}", algorithmName, e);
            throw new RuntimeException("指定算法排程执行失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 记录调度信息
     * @param scenario 生产场景
     * @param algorithm 算法
     * @param result 排程结果
     */
    private void logSchedulingInfo(ProductionScenario scenario, 
                                 APSAlgorithm algorithm, 
                                 SchedulingResult result) {
        
        log.info("调度信息汇总:");
        log.info("  生产场景: {}", scenario.getDescription());
        log.info("  选中算法: {} ({})", algorithm.getAlgorithmName(), algorithm.getAlgorithmDescription());
        log.info("  排程状态: {}", result.getStatus());
        log.info("  订单数量: {}", result.getMetrics() != null ? result.getMetrics().getTotalOrders() : 0);
        log.info("  平均资源利用率: {}", 
            result.getMetrics() != null ? result.getMetrics().getAverageResourceUtilization() : 0.0);
        log.info("  总完工时间: {}", 
            result.getMetrics() != null ? result.getMetrics().getTotalMakespan() : 0.0);
    }
}
